package cn.elead.chaos.email.service.impl;

import java.util.Date;

import javax.mail.MessagingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import cn.elead.chaos.email.domain.CustomMailMessage;
import cn.elead.chaos.email.handler.ISendHandler;
import cn.elead.chaos.email.service.IEmailService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EmailService extends EmailProcessor implements IEmailService {

	@Autowired
	private JavaMailSender mailSender;

	@Autowired
	private ISendHandler sendHandler;

	/**
	 * 发送普通邮件
	 */
	@Override
	public CustomMailMessage sendSimpleMail(CustomMailMessage message) {
		try {
			// 发送邮件发送前
			sendHandler.onSendBefore(message);		
			// 获取邮件主题和模板
			subjectContext(message);
			// 设置邮件消息发送的时间
			message.setSentDate(new Date());
			// 发送邮件
			mailSender.send(message);
			// 发送成功
			sendHandler.onSendSuccess(message);
		} catch (Exception e) {
			log.error("发送邮件失败:{}", e);
			sendHandler.onSendFailure(message, e);
		}
		return message;
	}

	/**
	 * 发送html格式邮件
	 */
	@Override
	public CustomMailMessage sendHtmlMail(CustomMailMessage message) throws MessagingException {
		try {
			message.setIfSuccess(true);
			// 发送邮件发送前
			sendHandler.onSendBefore(message);
			// true表示需要创建一个multipart message
			MimeMessageHelper helper = createMimeMessageHelper(mailSender);
			// 获取邮件主题和模板
			subjectContext(message);
			// 对象转换
			convert(message, helper);
			// 设置邮件消息发送的时间
			// message.setSentDate(new Date());
			// 发送邮件
			mailSender.send(helper.getMimeMessage());
			// 发送成功
			sendHandler.onSendSuccess(message);
		} catch (Exception e) {
			log.error("发送HTML邮件失败:{}", e);
			sendHandler.onSendFailure(message, e);
			message.setIfSuccess(false);
		}
		return message;
	}

	/**
	 * 发送带附件邮件
	 */
	@Override
	public CustomMailMessage sendAttachmentsMail(CustomMailMessage message) throws MessagingException {
		try {
			message.setIfSuccess(true);
			// 发送邮件发送前
			sendHandler.onSendBefore(message);
			// true表示需要创建一个multipart message
			MimeMessageHelper helper = createMimeMessageHelper(mailSender);
			// 获取邮件主题和模板
			subjectContext(message);
			// 对象转换
			convert(message, helper);
			// 添加附件
			attachment(message, helper);
			// 设置邮件消息发送的时间
			// message.setSentDate(new Date());
			// 发送邮件
			mailSender.send(helper.getMimeMessage());
			// 发送成功
			sendHandler.onSendSuccess(message);
		} catch (Exception e) {
			log.error("发送HTML邮件失败:{}", e);
			sendHandler.onSendFailure(message, e);
			message.setIfSuccess(false);
		}

		return message;
	}

	/**
	 * 发送正文中有静态资源（图片）的邮件
	 */
	@Override
	public CustomMailMessage sendInlineResourceMail(CustomMailMessage message) throws MessagingException {
		try {
			message.setIfSuccess(true);
			// 发送邮件发送前
			sendHandler.onSendBefore(message);			
			// true表示需要创建一个multipart message
			MimeMessageHelper helper = createMimeMessageHelper(mailSender);
			// 获取邮件主题和模板
			subjectContext(message);
			// 对象转换
			convert(message, helper);
			// 添加附件
			attachment(message, helper);
			// 添加静态资源
			resource(message, helper);
			// 发送邮件
			mailSender.send(helper.getMimeMessage());
			// 发送成功
			sendHandler.onSendSuccess(message);
		} catch (Exception e) {
			log.error("发送HTML邮件失败:{}", e);
			sendHandler.onSendFailure(message, e);
			message.setIfSuccess(false);
		}

		return message;

	}

}
